using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace TurbaconPhone
{
    public static class Graphics
    {
        static RenderTarget2D trailRD;
        static RenderTarget2D fadeRD;
        static GraphicsDeviceManager GDM;

        static Texture2D BG;

        public static Texture2D debugTexture;
        public static SpriteBatch Batch;

        public static void Initialize(GraphicsDeviceManager gdm)
        {
            GDM = gdm;

            trailRD = new RenderTarget2D(GDM.GraphicsDevice, GDM.PreferredBackBufferWidth, GDM.PreferredBackBufferHeight, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 4, RenderTargetUsage.PreserveContents);
            fadeRD = new RenderTarget2D(GDM.GraphicsDevice, GDM.PreferredBackBufferWidth, GDM.PreferredBackBufferHeight, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 4, RenderTargetUsage.DiscardContents);

            GDM.GraphicsDevice.SetRenderTarget(trailRD);
            GDM.GraphicsDevice.Clear(Color.Black);
            GDM.GraphicsDevice.SetRenderTarget(fadeRD);
            GDM.GraphicsDevice.Clear(Color.Black);
            GDM.GraphicsDevice.SetRenderTarget(null);

            BG = TurBacon.Content.GetTexture("soccer_field");

        }

        public static void Draw()
        {

            GDM.GraphicsDevice.Clear(Color.Black);
            GDM.GraphicsDevice.SetRenderTarget(trailRD);
            Batch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            
            //foreach (GameComponent comp in Program.Game.Components)
            //{
            //    GameObject obj = (GameObject)comp;
            //    if (!obj.HasTrail) continue;
            //    obj.DrawTrail(Batch);
            //}
            foreach (GameObject obj in GameplayState.gObjects)
            {
                if (!obj.HasTrail) continue;
                obj.DrawTrail(Batch);
            }
            Batch.End();

            GDM.GraphicsDevice.SetRenderTarget(fadeRD);
            Batch.Begin();
            Batch.Draw(trailRD, Vector2.Zero, new Color(new Vector3(0.96f, 0.96f, 0.96f)));
            Batch.End();

            GDM.GraphicsDevice.SetRenderTarget(trailRD);
            Batch.Begin();
            Batch.Draw(fadeRD, Vector2.Zero, Color.White);
            Batch.End();

            GDM.GraphicsDevice.SetRenderTarget(null);
            Batch.Begin();
            Batch.Draw(BG, new Rectangle(0, 0, GDM.PreferredBackBufferWidth, GDM.PreferredBackBufferHeight), Color.White);
            Batch.Draw(trailRD, Vector2.Zero, new Color(1f,1f,1f,0f));
            //foreach (GameComponent comp in Program.Game.Components)
            //{
            //    GameObject obj = (GameObject)comp;
            //    obj.Draw(Batch);
            //    //Batch.Draw(obj.Texture, obj.Pos, Color.White);
            //}
            foreach (GameObject obj in GameplayState.gObjects)
            {
                obj.Draw(Batch);
            }
            Batch.End();
        }
    }
}
